//
//  MineVM.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/23.
//

import Foundation
import Moya
import JKSwiftExtension

enum MineVM {
    case MineUseProfile
    case setMineUserProfile(avatar:String?,birthStr:String?,name:String?,sex:Int?)
    case uploadImage(image:Image?)
    case sendAppInPurchases(orderId:String?,pushchasesData:String?)
    case sendOrderNumber(orderType:Int?)
}

extension MineVM:TargetType {
    
    var baseURL: URL {
        return URL.init(string: Moya_baseURL)!
    }
    
    var path: String {
        switch self {
        case .MineUseProfile:
            return "api/app/user/getUserInfo"
        case .setMineUserProfile:
            return "api/app/user/saveUserInfo"
        case .uploadImage:
            return "api/admin/upload/tencentUpload"
        case .sendAppInPurchases:
            return "api/app/order/getPayResult"
        case .sendOrderNumber:
            return "api/app/order/addOrder"
        }
    }
    var method:Moya.Method {
        switch self {
        case .MineUseProfile:
            return .get
        case .setMineUserProfile,.uploadImage,.sendAppInPurchases,.sendOrderNumber:
            return .post
        }
    }
    
    var sampleData: Data {
        return Data()
    }
    
    
    var task: Task {
        switch self {
        case .MineUseProfile:
            return .requestPlain
        case let .setMineUserProfile(avatar, birthStr, name, sex):
            return .requestParameters(parameters: ["avatar":avatar ?? "","birthStr":birthStr ?? "","name":name ?? "","sex":sex ?? 0], encoding: JSONEncoding.default)
        case let .uploadImage(image):
            let data:Data = image?.jpegData(compressionQuality: 0.7) ?? Data()
            
            var dateStr:String =  SXDateTool.getNowTimeTimestamp(timeFormat: TimeFormat.YYYYMMDDHHMMSS)
            dateStr = dateStr.appendingFormat(".jpeg")
            let formData:MultipartFormData = MultipartFormData(provider:.data(data), name: "file",fileName: dateStr,mimeType: "image/jpeg")
            return .uploadMultipart([formData])
            
        case let .sendAppInPurchases(orderId,pushchasesData):
            return .requestParameters(parameters: ["receiptData":pushchasesData ?? "","orderId":orderId ?? ""], encoding: JSONEncoding.default)
        case let .sendOrderNumber(orderType):
            return .requestParameters(parameters: ["orderType":orderType ?? 0], encoding: JSONEncoding.default)
        }
    }
    
    var headers: [String : String]? {
        let tokenName:String = UserDefaults.standard.string(forKey: "LoginName") ?? ""
        let tokenValue:String = UserDefaults.standard.string(forKey: "LoginToken") ?? ""
        return [tokenName:tokenValue]
    }
    
    
}
